package com.zryc.shop.sms.controller;

import com.zryc.shop.base.Result;
import com.zryc.shop.sms.entity.HomeAdvertise;
import com.zryc.shop.sms.service.IHomeAdvertiseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.Arrays;

/**
 * <p>
 * 首页轮播广告表 前端控制器
 * </p>
 *
 * @author zhaozhao
 * @since 2024-01-18
 */
@RestController
@Api(tags = "广告列表管理")
@RequestMapping("/sms/home/advertise")
public class HomeAdvertiseController {

    @Autowired
    private IHomeAdvertiseService homeAdvertiseService;

    @GetMapping("/list")
    @ApiOperation("广告列表")
    public Result list(@RequestParam(required = false) @ApiParam(value = "广告名称关键词") String name,
                       @RequestParam(required = false) @ApiParam(value = "广告投放方式：0-PC首页 1-APP首页") Integer type,
                       @RequestParam(required = false) @ApiParam(value = "到期日期") String endTime,
                       @RequestParam(defaultValue = "1") @ApiParam(value = "页码", required = true) Integer pageNum,
                       @RequestParam(defaultValue = "10") @ApiParam(value = "每页显示条数", required = true) Integer pageSize) {
        return Result.ok(homeAdvertiseService.list(name, type, endTime, pageNum, pageSize));
    }

    @PostMapping("/create")
    @ApiOperation("新增广告")
    public Result create(@RequestBody HomeAdvertise advertise) {
        return Result.ok(homeAdvertiseService.save(advertise));
    }

    @PostMapping("/update/status/{id}")
    @ApiOperation("修改广告状态")
    public Result updateStatus(@PathVariable Long id, @RequestParam Integer status) {
        return Result.ok(homeAdvertiseService.updateStatus(id, status));
    }

    @GetMapping("/{id}")
    @ApiOperation("查询广告详情")
    public Result info(@PathVariable Long id) {
        return Result.ok(homeAdvertiseService.getById(id));
    }

    @PostMapping("/update/{id}")
    @ApiOperation("更新广告内容")
    public Result update(@PathVariable Long id, @RequestBody HomeAdvertise advertise){
        return Result.ok(homeAdvertiseService.updateAdvertise(id,advertise));
    }

    @PostMapping("/delete")
    @ApiOperation("删除广告位")
    public Result delete(@RequestParam String ids){
        String[] idList=ids.split(",");
        return Result.ok(homeAdvertiseService.removeByIds(Arrays.asList(idList)));
    }
}
